package br.com.poc.pitagorasnumero.view.activity;

import roboguice.activity.RoboFragmentActivity;
import roboguice.event.Observes;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import br.com.poc.pitagorasnumero.R;
import br.com.poc.pitagorasnumero.event.BroadcastEventManager;
import br.com.poc.pitagorasnumero.event.FacebookEvent;
import br.com.poc.pitagorasnumero.event.OnActivityResultEvent;
import br.com.poc.pitagorasnumero.presenter.IFacebookInitPresenter;
import br.com.poc.pitagorasnumero.socialnetwork.FacebookManager;

import com.google.inject.Inject;

/**
 * The Class MenuPrincipal.
 * 
 * @author irlan.gomes
 */
@ContentView(R.layout.main_menu_activity)
public class MainMenuActivity extends RoboFragmentActivity implements
		OnClickListener {

	/** The Constant BTN_NOME_COMPLETO. */
	private static final int BTN_NOME_COMPLETO = R.id.btn_nome_completo;

	/** The Constant BTN_NOME_FORMAL. */
	private static final int BTN_NOME_FORMAL = R.id.btn_nome_formal;

	/** The Constant BTN_DT_NASCIMENTO. */
	private static final int BTN_DT_NASCIMENTO = R.id.btn_dt_nascimento;

	/** The Constant BTN_CASA. */
	private static final int BTN_CASA = R.id.btn_casa;

	/** The Constant BTN_CARRO. */
	private static final int BTN_CARRO = R.id.btn_carro;

	/** The Constant BTN_EU_ELA. */
	private static final int BTN_EU_ELA = R.id.btn_eu_ela;

	/** The Constant BTN_FACEBOOK. */
	private static final int BTN_FACEBOOK = R.id.btn_auth_facebook;

	/** The btn nome completo. */
	@InjectView(BTN_NOME_COMPLETO)
	private Button btnNomeCompleto;

	/** The btn nome formal. */
	@InjectView(BTN_NOME_FORMAL)
	private Button btnNomeFormal;

	/** The btn dt nascimento. */
	@InjectView(BTN_DT_NASCIMENTO)
	private Button btnDtNascimento;

	/** The btn casa. */
	@InjectView(BTN_CASA)
	private Button btnCasa;

	/** The btn carro. */
	@InjectView(BTN_CARRO)
	private Button btnCarro;

	/** The btn eu ela. */
	@InjectView(BTN_EU_ELA)
	private Button btnEuEla;

	/** The btn facebook. */
	@InjectView(BTN_FACEBOOK)
	private Button btnFacebook;

	/** The facebook bo. */
	@Inject
	private IFacebookInitPresenter facebookInitPresenter;

	/** The bem. */
	@Inject
	private BroadcastEventManager bem;

	/*
	 * (non-Javadoc)
	 * 
	 * @see roboguice.activity.RoboFragmentActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setOnClickListener();

		this.facebookInitPresenter.initFacebook(this);

		changeUIFacebook();
	}

	private void setOnClickListener() {
		btnNomeCompleto.setOnClickListener(this);
		btnNomeFormal.setOnClickListener(this);
		btnDtNascimento.setOnClickListener(this);
		btnCasa.setOnClickListener(this);
		btnCarro.setOnClickListener(this);
		btnEuEla.setOnClickListener(this);
		btnFacebook.setOnClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(final View v) {
		switch (v.getId()) {
		case BTN_NOME_COMPLETO:
			startActivity(NameFullActivity.class);
			break;

		case BTN_NOME_FORMAL:
			startActivity(NameFormalActivity.class);
			break;

		case BTN_DT_NASCIMENTO:
			startActivity(DateBirthActivity.class);
			break;

		case BTN_CASA:
			startActivity(HouseActivity.class);
			break;

		case BTN_CARRO:
			startActivity(CarActivity.class);
			break;

		case BTN_EU_ELA:
			startActivity(MeAndSheActivity.class);
			break;

		case BTN_FACEBOOK:
			startFacebook();
			break;
		default:
			break;
		}

	}

	private void startFacebook() {
		if (facebookInitPresenter.isFacebookLoggedIn()) {
			facebookInitPresenter.logoutFacebook();
		} else {
			facebookInitPresenter.doAuthenticationFacebook();
		}
	}

	private void changeUIFacebook() {
		if (facebookInitPresenter.isFacebookLoggedIn()) {
			btnFacebook.setText("Sair Facebook");
		} else {
			btnFacebook.setText("Entrar Facebook");
		}
	}

	protected void doneFacebookOperation(@Observes final FacebookEvent event) {
		changeUIFacebook();
	}

	/**
	 * Start activity.
	 * 
	 * @param intentClass
	 *            the intent class
	 */
	private void startActivity(final Class<?> intentClass) {
		final Intent intent = new Intent(getApplicationContext(), intentClass);
		startActivity(intent);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see roboguice.activity.RoboFragmentActivity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == FacebookManager.REQUEST_CODE) {
			bem.fireEvent(new OnActivityResultEvent(requestCode, resultCode,
					data));
		}
	}

}
